let CommentModel = require('../model').commentModel;
let DaoTool = require('../lib/daoTool');

exports.createComment = function (conn, params) {
    return new Promise(function (resolve, reject) {
        let sql = "insert into comment set ?";
        conn.query(sql, [DaoTool.generateModelObject(params, CommentModel, 'create')], function (err, result) {
            printLog("=createComment=", this.sql);
            if (err) reject(err);
            resolve(result)
        });
    });
};
exports.deleteComment = function (conn, params) {
    return new Promise(function (resolve, reject) {
        conn.query("delete from comment where ToTopic=?;", [params.TopicId], function (err, result) {
            printLog("=deleteComment=", this.sql);
            if (err) reject(err);
            resolve(result)
        });
    });
};
exports.pcDeleteComment = function (conn, params) {
    return new Promise(function (resolve, reject) {
        conn.query("delete from comment where CommentId=?;", [params.CommentId], function (err, result) {
            printLog("=deleteComment=", this.sql);
            if (err) reject(err);
            resolve(result)
        });
    });
};
exports.selectComment = function (conn, params) {
    return new Promise(function (resolve, reject) {
        let sql = "select cmt.*,mu.NickName,tu.NickName as ToNickName from comment as cmt left JOIN user as mu on mu.openid=cmt.openid left join comment as tcmt on tcmt.CommentId=cmt.ToComment left join user as tu on tu.openid=tcmt.openid where cmt.ToTopic in(?) order by cmt.CommentId asc";
        conn.query(sql, [params], function (err, result) {
            printLog("=selectComment err=", err);
            printLog("=selectComment=", this.sql);
            if (err) reject(err);
            resolve(result)
        });
    });
};
exports.pcCommentList = function (conn, params, isCount) {
    return new Promise(function (resolve, reject) {
        let sql = " from comment as cmt left JOIN user as mu on mu.openid=cmt.openid left join comment as tcmt on tcmt.CommentId=cmt.ToComment left join user as tu on tu.openid=tcmt.openid " + getSelectCommentWhere(params) + " order by cmt.CommentId asc";
        let selectSql = "select cmt.*,mu.NickName,tu.NickName as ToNickName" + sql + DaoTool.getPageParamsSqlStr(params);
        let countSql = 'select count(1) as num' + sql;
        conn.query(selectSql, [params.ToTopic], function (err, selectResult) {
            printLog("=pcCommentList err=", err);
            printLog("=pcCommentList=", this.sql);
            if (err) reject(err);
            if (!isCount) resolve(selectResult);
            conn.query(countSql, [params.ToTopic], function (err, countResult) {
                printLog("=pcCommentList err=", err);
                printLog("=pcCommentList=", this.sql);
                if (err) reject(err);
                resolve({data: selectResult, count: countResult[0].num});
            });
        });
    });
};
function getSelectCommentWhere(params) {
    let sql = "";
    sql += params.ToTopic ? " and cmt.ToTopic = " + params.ToTopic : "";
    sql += params.CommentContent ? " and cmt.CommentContent like " + mysqlHelper.escape('%' + params.CommentContent + '%') : "";
    sql += params.NickName ? ' and tu.NickName like ' + mysqlHelper.escape('%' + params.NickName + '%') : "";
    sql += params.UserNo ? " and tu.UserNo like " + mysqlHelper.escape('%' + params.UserNo + '%') : "";
    sql += params.Province ? " and tu.Province = " + params.Province : "";
    sql += params.City ? " and tu.City = " + params.City : "";
    if (sql != '')
        sql = ' where ' + sql.substring(4);
    return sql;
}